package com.ophiux.utils.common;

/**
 * 数组工具类
 * 
 * @author chenh
 */
public class ArraysUtil {

	/**
	 * 
	 * @param content   需要转为数组的内容
	 * @param separator 符号
	 * @return
	 * @throws Throwable
	 */
	public static String[] toArray(String content, String separator) {

		if (StringUtil.isEmpty(content))
			return null;

		try {
			String array[] = content.split(separator); // 根据指定符号进行拆分
			return array;
		} catch (Exception e) {
			throw new RuntimeException(e.getCause());
		}
	}

	/**
	 * 数据清洗，去除最后一个符号,或最前面的符号
	 * 
	 * @param content   内容
	 * @param separator 分割符号
	 * @return
	 */
	public static String clean(String content, String separator) {
		// 不为空，判断最后是否包含分隔符","若存在，则去除
		if (StringUtil.isEmpty(content))
			return "";
		try {
			do {
				if (isStandard(content, separator)) {
					return content;
				}

				// 第一个分隔符出现的位置
				if (content.indexOf(",") == 0) {
					content = content.substring(1, content.length());
				}

				if (content.lastIndexOf(",") == content.length() - 1) {
					content = content.substring(0, content.length() - 1);
				}

			} while (true);

		} catch (Exception e) {
			e.printStackTrace();
			return content;
		}
	}

	/**
	 * 是否合格,字符首位没得 separator 分隔符时合格
	 * 
	 * @return true：合格 false：不合格
	 */
	public static boolean isStandard(String content, String separator) {
		if (content.indexOf(",") == 0 || content.lastIndexOf(",") == content.length() - 1) {
			return false;
		}
		return true;
	}

	public static void main(String[] args) {
		String DEALTYPE = ",,,,,0,1,,,,,,";
		if (DEALTYPE.lastIndexOf(",") == DEALTYPE.length() - 1) {
			DEALTYPE = clean(DEALTYPE, ",");
		}
		System.out.println(DEALTYPE);
	}
}
